{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "26d16a65",
   "metadata": {},
   "source": [
    "# 1.音频录制\n",
    "\n",
    "安装和下载模块：pip install 名称\n",
    "> 1. SpeechRecognition\n",
    "> 2. Pyaudio\n",
    "> 3. pydud"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4769adf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: SpeechRecognition in c:\\users\\jenny\\appdata\\roaming\\python\\python39\\site-packages (3.10.0)\n",
      "Requirement already satisfied: requests>=2.26.0 in d:\\anaconda\\lib\\site-packages (from SpeechRecognition) (2.27.1)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in d:\\anaconda\\lib\\site-packages (from requests>=2.26.0->SpeechRecognition) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in d:\\anaconda\\lib\\site-packages (from requests>=2.26.0->SpeechRecognition) (3.3)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\\anaconda\\lib\\site-packages (from requests>=2.26.0->SpeechRecognition) (1.26.9)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\anaconda\\lib\\site-packages (from requests>=2.26.0->SpeechRecognition) (2021.10.8)\n"
     ]
    }
   ],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "707eda1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: PyAudio in c:\\users\\jenny\\appdata\\roaming\\python\\python39\\site-packages (0.2.13)\n"
     ]
    }
   ],
   "source": [
    "!pip install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f26c163c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: pydub in c:\\users\\jenny\\appdata\\roaming\\python\\python39\\site-packages (0.25.1)\n"
     ]
    }
   ],
   "source": [
    "!pip install pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "157c3d22",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca69bd5a",
   "metadata": {},
   "source": [
    "# 2.语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b58c137",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "951b1b83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.567628\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'这样子。'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result  = eval(bdasr.asr('1.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc2c5691",
   "metadata": {},
   "source": [
    "# 3. 连接自动回复字典（openai-chatgpt）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b4549da5",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你是谁？\":\"我是集美貌和智慧于一体的智能小网同学\",\n",
    "    '你好。':\"你也好，今天听起来心情不错哦\",\n",
    "    '广州天气。':\"今天有雨\",\n",
    "    '这样子。':'哪样子',\n",
    "    '北京科技馆。':\"中国科学技术馆，位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3fa04a0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'哪样子'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e8a9955d",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 3.1接入chatgpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0e680aff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: openai in c:\\users\\jenny\\appdata\\roaming\\python\\python39\\site-packages (0.27.6)\n",
      "Requirement already satisfied: aiohttp in d:\\anaconda\\lib\\site-packages (from openai) (3.8.1)\n",
      "Requirement already satisfied: requests>=2.20 in d:\\anaconda\\lib\\site-packages (from openai) (2.27.1)\n",
      "Requirement already satisfied: tqdm in d:\\anaconda\\lib\\site-packages (from openai) (4.64.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in d:\\anaconda\\lib\\site-packages (from requests>=2.20->openai) (3.3)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\\anaconda\\lib\\site-packages (from requests>=2.20->openai) (1.26.9)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in d:\\anaconda\\lib\\site-packages (from requests>=2.20->openai) (2.0.4)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\anaconda\\lib\\site-packages (from requests>=2.20->openai) (2021.10.8)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in d:\\anaconda\\lib\\site-packages (from aiohttp->openai) (1.2.0)\n",
      "Requirement already satisfied: attrs>=17.3.0 in d:\\anaconda\\lib\\site-packages (from aiohttp->openai) (21.4.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in d:\\anaconda\\lib\\site-packages (from aiohttp->openai) (5.1.0)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in d:\\anaconda\\lib\\site-packages (from aiohttp->openai) (4.0.1)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in d:\\anaconda\\lib\\site-packages (from aiohttp->openai) (1.6.3)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in d:\\anaconda\\lib\\site-packages (from aiohttp->openai) (1.2.0)\n",
      "Requirement already satisfied: typing-extensions>=3.6.5 in d:\\anaconda\\lib\\site-packages (from async-timeout<5.0,>=4.0.0a3->aiohttp->openai) (4.1.1)\n",
      "Requirement already satisfied: colorama in d:\\anaconda\\lib\\site-packages (from tqdm->openai) (0.4.4)\n"
     ]
    }
   ],
   "source": [
    "!pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ec122c41",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "22d2663a",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'opchat' has no attribute 'openai_chat'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[1;32mIn [30]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mopchat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopenai_chat\u001b[49m(bdasr_result)\n\u001b[0;32m      2\u001b[0m response\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'opchat' has no attribute 'openai_chat'"
     ]
    }
   ],
   "source": [
    "response = opchat.openai_chat(bdasr_result)\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bd09ec2",
   "metadata": {},
   "source": [
    "# 4. 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2600ae67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.41f79fe97a43a78dc0b283a621838b34.2592000.1687091596.282335-19331335 ; EXPIRES IN SECONDS: 2592000\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.41f79fe97a43a78dc0b283a621838b34.2592000.1687091596.282335-19331335&tex=%25E5%2593%25AA%25E6%25A0%25B7%25E5%25AD%2590&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a67e49a",
   "metadata": {},
   "source": [
    "# 5.播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "357eda0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68bcc423",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73e58f88",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7712d666",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
